//
//  WQSceneMainViewController.m
//  WisdomQuestor
//
//  Created by 纪景城 on 2024/12/18.
//

#import "WQSceneMainViewController.h"
#import "WQOneClickExecutionViewController.h"
#import <ThingSmartBizCore/ThingSmartBizCore.h>
#import <ThingModuleServices/ThingSmartSceneProtocol.h>
#import <ThingModuleServices/ThingModuleServices.h>
#import <ThingSmartDeviceKit/ThingSmartDeviceKit.h>

@interface WQSceneMainViewController () <JXCategoryListContainerViewDelegate>

@property (nonatomic, strong) UIImageView           *backColorImageView;

@property (nonatomic, strong) UIButton           *logoButton;
@property (nonatomic, strong) UIButton           *framilyButton; // 家庭

@property (nonatomic, strong) UIButton           *rightMicrButton; // 麦克风
@property (nonatomic, strong) UIButton           *rightAddButton; // +

@property (nonatomic, strong) UIView           *noDataView; // 没数据的view

@property (nonatomic, strong) UIView           *haveDataView; // 有数据的view
@property (nonatomic, strong) JXCategoryTitleView *jk_categoryView; // 分页
@property (nonatomic, strong) JXCategoryListContainerView *jk_listContainerView;

@end

@implementation WQSceneMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeAll;
    self.hbd_barTintColor = UIColor.clearColor;
    self.hbd_barAlpha = 0;
    
    self.navigationItem.leftBarButtonItems = @[
        [[UIBarButtonItem alloc] initWithCustomView:self.logoButton],
        [[UIBarButtonItem alloc] initWithCustomView:self.framilyButton]
    ];
    
    self.navigationItem.rightBarButtonItems = @[[[UIBarButtonItem alloc] initWithCustomView:self.rightAddButton], [[UIBarButtonItem alloc] initWithCustomView:self.rightMicrButton]];
    
    [self.view addSubviews:[NSArray arrayWithObjects:self.backColorImageView, self.noDataView, self.haveDataView, nil]];
    [self.view insertSubview:self.backColorImageView atIndex:0];
    [self.haveDataView addSubviews:[NSArray arrayWithObjects:self.jk_categoryView, self.jk_listContainerView, nil]];
    [self fragmentUI_ViewSet];
    [self reloadSceneMainMethods];
    
    [[ThingSmartBizCore sharedInstance] registerService:@protocol(ThingSmartHomeDataProtocol) withInstance:self];
    [[ThingSmartBizCore sharedInstance] registerService:@protocol(ThingSmartHouseIndexProtocol) withInstance:self];
    [[ThingSmartBizCore sharedInstance] registerService:@protocol(ThingFamilyProtocol) withInstance:self];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadSceneMainMethods) name:kNotificationFamilyUpdate object:nil];
    
    /*
     kNotificationSmartSceneListUpdate，通知发送事件： 添加场景成功 编辑场景成功 删除场景成功（UI 业务包发送的默认场景类型为 ThingSmartSceneRecommendTypeNone 和 ThingSmartSceneCollectionTypeNone）
     kNotificationSmartSceneSaved，通知发送事件： 添加场景成功 编辑场景成功
     kNotificationSmartSceneRecomDeleted，通知发送事件：推荐场景或者收藏场景删除成功
     */
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadMethods) name:@"kNotificationSmartSceneListUpdate" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadMethods) name:@"kNotificationSmartSceneSaved" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadMethods) name:@"kNotificationSmartSceneRecomDeleted" object:nil];
    
    
    
}

- (ThingSmartHome *)getCurrentHome {
    ThingSmartHome *home = [ThingSmartHome homeWithHomeId:WQShare.userHomeModel.homeId];
    return home;
}
- (BOOL)homeAdminValidation {
    return YES;
}
- (long long)currentFamilyId {
    return WQShare.userHomeModel.homeId;
}
- (BOOL)checkAdminAndRightLimit:(BOOL)alert {
   return YES;
}

#pragma mark ------- Private Methods -------

/// 刷新场景
- (void)reloadSceneMainMethods {
    if (WQShare.isLogin == NO) {
        [self.framilyButton setTitle:@"" forState:0];
        return;
    }
        
    [self.framilyButton setTitle:[NSString stringWithFormat:@"  %@", WQShare.userHomeModel.homeModel.name] forState:0];
}

- (void)reloadMethods {
    DEBUGLOG(@"reloadMethods");
}

/// 新增场景
- (void)gotoAddScene {
    
    if (WQShare.isLoginUserAndPresent) {
        
        id<ThingSmartSceneProtocol> impl = [[ThingSmartBizCore sharedInstance] serviceOfProtocol:@protocol(ThingSmartSceneProtocol)];
        [impl addAutoScene:^(ThingSmartSceneModel *secneModel, BOOL addSuccess) {
            DEBUGLOG(@"addAutoScene");
        }];
    }
}

#pragma mark ------- JXCategoryListContainerViewDelegate -------

//返回列表的数量
- (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
    return self.jk_categoryView.titles.count;
}

//根据下标index返回对应遵从`JXCategoryListContentViewDelegate`协议的列表实例
- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {
    
    switch (index) {
        case 0:
        {
            WQOneClickExecutionViewController *vc = [[WQOneClickExecutionViewController alloc] init];
            
            return vc;
        }
            break;

        default:
        {
            WQOneClickExecutionViewController *vc = [[WQOneClickExecutionViewController alloc] init];
            
            return vc;
        }
            break;
    }
}

#pragma mark ------- Lazy Methods -------

- (void)fragmentUI_ViewSet {
    [self.backColorImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.view);
        make.height.mas_equalTo(kFragmemtFloat(513));
    }];
    [self.noDataView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    [self.haveDataView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    [self.jk_categoryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.haveDataView);
        make.right.equalTo(self.haveDataView.mas_right).offset(-kFragmemtFloat(50));
        make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
        make.height.mas_equalTo(kFragmemtFloat(30));
    }];
    [self.jk_listContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.haveDataView);
        make.top.equalTo(self.jk_categoryView.mas_bottom).offset(kFragmemtFloat(10));
    }];
}

- (UIImageView *)backColorImageView {
    if (!_backColorImageView) {
        _backColorImageView = [[UIImageView alloc] initWithImage:kImageName(@"scene_colorbg_image")];
        _backColorImageView.contentMode = UIViewContentModeScaleAspectFill;
        _backColorImageView.userInteractionEnabled = YES;
    }
    return _backColorImageView;
}

- (UIButton *)logoButton {
    if (!_logoButton) {
        _logoButton = [UIButton initWithImage:kImageName(@"base_nav_logo") selectedImage:nil];
    }
    return _logoButton;
}

- (UIButton *)framilyButton {
    if (!_framilyButton) {
        _framilyButton = [UIButton initWithImage:kImageName(@"base_nav_framily") selectedImage:nil];
        _framilyButton.titleLabel.font = kAdaptedMediumFontSize(15);
        [_framilyButton setTitleColor:k_Color_222222 forState:0];
        
        [_framilyButton kj_addAction:^(UIButton * _Nonnull kButton) {
            
            [WQShare showExchangeFramilyListView];
        }];
    }
    return _framilyButton;
}

- (UIButton *)rightMicrButton {
    if (!_rightMicrButton) {
        _rightMicrButton = [UIButton initWithImage:kImageName(@"scene_nav_list") selectedImage:nil];
    }
    return _rightMicrButton;
}

- (UIButton *)rightAddButton {
    if (!_rightAddButton) {
        _rightAddButton = [UIButton initWithImage:kImageName(@"scene_nav_add") selectedImage:nil];
        
        kWEAKSELF
        [_rightAddButton kj_addAction:^(UIButton * _Nonnull kButton) {
            [WQShare addSceneMainMethods];
        }];
    }
    return _rightAddButton;
}

- (UIView *)noDataView {
    if (!_noDataView) {
        _noDataView = [[UIView alloc] init];
        _noDataView.backgroundColor = UIColor.clearColor;
    }
    return _noDataView;
}

- (UIView *)haveDataView {
    if (!_haveDataView) {
        _haveDataView = [[UIView alloc] init];
        _haveDataView.backgroundColor = UIColor.clearColor;
    }
    return _haveDataView;
}

- (JXCategoryTitleView *)jk_categoryView {
    if (!_jk_categoryView) {
        _jk_categoryView = [[JXCategoryTitleView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        _jk_categoryView.titles = @[@"一键执行", @"自动化"];
        _jk_categoryView.averageCellSpacingEnabled = NO;
        _jk_categoryView.titleColor = k_Color_94959A;
        _jk_categoryView.titleSelectedColor = k_Color_222222;
        _jk_categoryView.titleFont = kAdaptedMediumFontSize(18);
    }
    return _jk_categoryView;
}

- (JXCategoryListContainerView *)jk_listContainerView {
    if (!_jk_listContainerView) {
        _jk_listContainerView = [[JXCategoryListContainerView alloc] initWithType:JXCategoryListContainerType_ScrollView delegate:self];
        _jk_listContainerView.backgroundColor = UIColor.clearColor;
        self.jk_categoryView.listContainer = _jk_listContainerView;
    }
    return _jk_listContainerView;
}

@end
